package com.cb.listener;

import com.alibaba.fastjson2.JSONObject;
import com.cb.mapper.BookingMapper;
import com.cb.mapper.CoordinateMapper;
import com.jia.entity.Booking;
import com.rabbitmq.client.Channel;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;

@Component
public class DeadListener {

    @Autowired
    private BookingMapper bookingMapper;

    @Autowired
    private CoordinateMapper coordinateMapper;

    @RabbitListener(queues = "booking_dead_queue")
    public void receive(Message message,Channel channel) {
        String msg = new String(message.getBody());
        Booking booking = JSONObject.parseObject(msg, Booking.class);
        Integer v = booking.getV();
        Booking booking1 = bookingMapper.selectById(booking.getId());
        if(booking1.getState() == 0) {
            if (v == 1) {
                coordinateMapper.updateupf(booking.getCid());
            } else {
                coordinateMapper.updateups(booking.getCid());
            }
            bookingMapper.updateState(booking.getUid(), booking.getCid());
        }
        try {
            channel.basicAck(message.getMessageProperties().getDeliveryTag(),true);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}
